from PySide2.QtWidgets import QHBoxLayout, QLabel, QComboBox, QCheckBox, QLineEdit, QSizePolicy, QPushButton

from common.pyside2_table.qtable import Base_Table
from common.config import public_data


class Review_Table(Base_Table):
    def __init__(self, tb_name="", col_model_list=None, find_condition="status<3"):
        super().__init__(tb_name, col_model_list, find_condition=find_condition)
        self.col_model_list: list = col_model_list  # 数据列的控件对象列表 # 只占一行的列名
        self.reset_data()



class Header_Select_Layout:
    def __init__(self, layout: QHBoxLayout, change_fun=None, ck_txt: str = None, ck_col_name: str = None):
        self.lb_project = QLabel("项目")
        self.cmb_project = QComboBox()
        self.lb_lot = QLabel("采购包")
        self.cmb_lot_code = QComboBox()
        self.lb_factory = QLabel("制造厂")
        self.cmb_factory = QComboBox()
        self.ck_col_name = ck_col_name
        if ck_txt:
            self.ck = QCheckBox(text=ck_txt)
        wgt_list = [self.lb_project, self.cmb_project, self.lb_lot, self.cmb_lot_code, self.lb_factory,
                    self.cmb_factory]
        for item in wgt_list:
            layout.addWidget(item)
        if ck_txt:
            layout.addWidget(self.ck)
            self.ck.clicked.connect(change_fun)
        set_cmb_items(self.cmb_project, public_data.Project_list, change_fun)
        set_cmb_items(self.cmb_factory, public_data.Factory_list, change_fun)
        set_cmb_items(self.cmb_lot_code, public_data.Lot_list, change_fun)

    def get_change_status(self):
        lot_code = self.cmb_lot_code.currentText()
        factory = self.cmb_factory.currentText()
        project = self.cmb_project.currentText()
        selected_dic = dict()
        if self.cmb_project.isEnabled() and project != "所有":
            selected_dic["project"] = project
        if self.cmb_lot_code.isEnabled() and lot_code != "所有":
            selected_dic["lot_code"] = lot_code
        if self.cmb_factory.isEnabled() and factory != "所有":
            selected_dic["factory"] = factory

        if self.ck_col_name:
            if self.ck.isChecked():
                selected_dic[self.ck_col_name] = 1
        return selected_dic


def set_cmb_items(cmb: QComboBox, value_list: list, change_fun):
    if value_list:
        cmb.addItems(value_list)
    if len(value_list) == 1:
        cmb.setEnabled(False)
    else:
        cmb.currentTextChanged.connect(change_fun)


class Header_search_Layout:
    def __init__(self, layout: QHBoxLayout):
        self.txt_search = QLineEdit()
        self.txt_search.setPlaceholderText("请输入查询条件，模糊查询用*标识")
        self.txt_search.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)

        self.btn_db_search = QPushButton("搜索")
        wgt_list = [self.txt_search, self.btn_db_search]
        for item in wgt_list:
            layout.addWidget(item)

    def get_search_txt_to_sql(self):
        search_str: str = self.txt_search.text()
        return search_str.strip()
